package com.jbit.dm212.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReUtil;
import com.jbit.dm212.common.*;
import com.jbit.dm212.entity.dto.DmUserDTO;
import com.jbit.dm212.entity.dto.UserToken;
import com.jbit.dm212.entity.po.DmUser;
import com.jbit.dm212.entity.vo.DmUserVO;
import com.jbit.dm212.service.DmUserService;
import com.jbit.dm212.service.RedisService;
import net.sf.jsqlparser.expression.UserVariable;
import org.apache.catalina.mbeans.UserMBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/user/api/p")
public class DmUserController {
    @Resource
    private RedisService redisService;
    @Resource
    private DmUserService userService;

    @Resource
    private RedisUtils redisUtils;
    @PostMapping("/code")
    public ResponseResult code(@RequestBody DmUserVO userVO){
        String phone = userVO.getPhone();
        try {
            boolean flag = userService.checkPhone(phone);
            if (flag){
                return ResponseResultUtils.sendError("1003");
            }else{
                if (redisService.checkSms(phone)){
                    return ResponseResultUtils.sendError("1007");
                }else{
                    String code = SmsSendUtil.sendSms(phone);
                    redisService.sendSms(phone,code);
                    return ResponseResultUtils.sendSuccess(null);
                }
            }
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return ResponseResultUtils.sendError("1007",null);
    }

    @PostMapping("/checkEmail")
    public ResponseResult checkEmail(String phone){
        boolean flag = userService.checkPhone(phone);
        if (flag){
            return ResponseResultUtils.sendError("1003");
        }else{
            return ResponseResultUtils.sendSuccess();
        }
    }

    @PostMapping("/register")
    public ResponseResult register(@RequestBody DmUserVO userVO){
        DmUser user = new DmUser();
        String phone = userVO.getPhone();
        String code = userVO.getVcode();
        if (userService.checkPhone(phone)){
            return ResponseResultUtils.sendError("1003");
        }else{
            if (redisService.getCode(phone).equals(code)){
                BeanUtil.copyProperties(userVO,user);
                boolean flag = userService.register(user);
                if (flag)
                    redisService.delPhone(phone);
                return ResponseResultUtils.sendSuccess();
            }else{
                return ResponseResultUtils.sendError("1004");
            }
        }
    }

    @PostMapping("/login")
    public ResponseResult login(@RequestBody DmUserVO userVO){
        DmUser user = BeanUtil.toBean(userVO,DmUser.class);
        DmUserDTO userDTO = userService.login(user);
        if (userDTO != null){
            String checkPwd = MD5Util.digest(user.getPassword());
            if (userDTO.getUser().getPassword().equals(checkPwd)){
                //保存token
                Object[] objects = new Object[2];
                objects[0] = userDTO.getUser();
                UserToken token = new UserToken();
                token.setToken(UUID.randomUUID() + "_" + userDTO.getUser().getPhone());
                token.setGenTime(System.currentTimeMillis());
                token.setExpTime(token.getGenTime() + 3600000);
                objects[1] = token;
                redisUtils.set(token.getToken(),userDTO.getUser(),3600);
                return ResponseResultUtils.sendSuccess(objects);
            }
        }
        return ResponseResultUtils.sendError("1006");
    }
}
